package com.jzo2o.customer.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags="用户端－地址薄相关接口")
public class AddressBookControlle {
    @Resource
    private IAddressBookService addressBookService;
    @ApiOperation("地址薄新增")
    @PostMapping()
    public void add(@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.add(addressBookUpsertReqDTO);
    }
    @PutMapping("/{id}")
    @ApiOperation("地址薄修改")
    public void update(@PathVariable Long id,@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO){
        addressBookService.update(id, addressBookUpsertReqDTO);
    }
    @GetMapping("/{id}")
    @ApiOperation("地址薄详情")
    public AddressBookResDTO detail(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtil.toBean(addressBook,AddressBookResDTO.class);
    }
    @PutMapping("/default")
    @ApiOperation("地址薄设为默认/取消默认")
    public void updateDefaultStatus( @RequestParam Long id,@RequestParam Integer flag) {
        Long userId = UserContext.currentUserId();
        addressBookService.updateDefaultStatus(userId,id, flag);
    }
    @DeleteMapping("/batch")
    @ApiOperation("地址薄批量删除")
    public void logicallyDelete(@RequestBody List<Long> ids){
        addressBookService.removeByIds(ids);
    }
    @GetMapping("/page")
    @ApiOperation("地址薄分页查询")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO){
        return addressBookService.page(addressBookPageQueryReqDTO);
    }
    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBookResDTO defaultAddress() {
        return addressBookService.defaultAddress();
    }
}
